package com.gp.gback.system.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.jboss.logging.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class LogAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
    private static final String SERVICE_POINTCUT = "execution(* *..service*..*(..)))";

    @Around(SERVICE_POINTCUT)
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        final String CLASS_NAME = joinPoint.getTarget().getClass().getSimpleName();
        final String METHOD_NAME = joinPoint.getSignature().getName();
        long startTime = System.currentTimeMillis();
        Object[] args = joinPoint.getArgs();

        String logFileName = joinPoint.getTarget().getClass().getName()
                .replace("com.fp.fpos.system.","").split("\\.")[0];
        MDC.put("logFileName",logFileName);
        LOGGER.info("In Function: {}.{}", new Object[]{CLASS_NAME, METHOD_NAME});
        Object obj = joinPoint.proceed(args);
        long endTime = System.currentTimeMillis();
        float timeSpend = (endTime - startTime)/1000F;
        LOGGER.info("END Function: {}.{} ;spend {} seconds", new Object[]{CLASS_NAME, METHOD_NAME,timeSpend});
        MDC.remove("logFileName");
        
        return obj;
    }
}
